package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuCommunity;
import com.hualan.dao.CommunityDao;
import com.hualan.dao.impl.CommunityDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/communityInsertServlet")
public class CommunityInsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(req.getParameter("community_id"));
        String name = req.getParameter("community_name");
        System.out.println(id);
        String address = req.getParameter("community_address");
        String province = req.getParameter("community_province");
        String city = req.getParameter("community_city");
        String district = req.getParameter("community_district");
        int areaCode = Integer.parseInt(req.getParameter("community_area_code"));
        String latitude = req.getParameter("community_latitude");
        String longitude = req.getParameter("community_longitude");
        int status = Integer.parseInt(req.getParameter("community_status"));
        String coordinates = req.getParameter("community_coordinates");
        PanghuCommunity community = new PanghuCommunity(id,name,address,province,city,district,areaCode,latitude,longitude,status,coordinates);
        CommunityDao communityDao = new CommunityDaoImpl();
        int row = communityDao.addCommunity(community);
        Map<String,Object> map = new HashMap<>();
        if (row < 1){
            map.put("code", 300);
            map.put("message", "更新失败");
        }else {
            map.put("code", 200);
            map.put("message", "更新成功");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }
}
